package com.kunpeng.weibo.sina.api;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kunpeng.weibo.sina.SinaWeibo;
import com.kunpeng.weibo.sina.beans.Oauth2AccessToken;
import com.kunpeng.weibo.sina.beans.WeiboParameters;
import com.kunpeng.weibo.sina.http.HttpUtil;
import com.kunpeng.weibo.sina.utils.AuthorizeInterface;

/**
 * 认证相关接口
 * 
 * @author Lee
 * @date 2012-4-23
 * @Description TODO
 */
public class Authorize_API {

	private WebView				mWebView;

	private AuthorizeInterface	mAuthorizeInterface;

	/**
	 * 认证
	 * 
	 * @param webview 打开认证页面的WebView
	 * @param authorizeInterface 认证的回调接口
	 */
	public void authorize(WebView webview, AuthorizeInterface authorizeInterface) {
		if (webview == null) {
			throw new NullPointerException();
		}
		mWebView = webview;
		mAuthorizeInterface = authorizeInterface;
		setWebViewClient();

		WeiboParameters params = new WeiboParameters();
		params.add("client_id", SinaWeibo.SINA_APP_KEY);
		params.add("response_type", "token");
		params.add("redirect_uri", SinaWeibo.REDIRECT_URI_CALLBACK);
		params.add("display", "mobile");
		String url = "https://api.weibo.com/oauth2/authorize?" + HttpUtil.encodeUrl(params);
		if (URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) {
			mWebView.loadUrl(url);
		} else {
			if (mAuthorizeInterface != null) {
				mAuthorizeInterface.authorizeFailed("获取认证URL错误");
			}
		}
	}

	private void setWebViewClient() {
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setDefaultTextEncodingName("utf-8");
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				Log.i("lijinzhe", "onPageStarted   url:" + url);
				if (url.startsWith(SinaWeibo.REDIRECT_URI_CALLBACK)) {
					view.stopLoading();
					Bundle values = HttpUtil.parseUrl(url);

					String error = values.getString("error");
					String error_code = values.getString("error_code");

					if (error == null && error_code == null) {
						String token = values.getString("access_token");
						Oauth2AccessToken accessToken = new Oauth2AccessToken();
						accessToken.setAccessToken(token);
						accessToken.setOauthTokenSecret(SinaWeibo.SINA_APP_SECRET);
						SinaWeibo.getInstance().setAccessToken(accessToken);
						if (mAuthorizeInterface != null) {
							mAuthorizeInterface.authorizeSuccessful(accessToken);
						}
					} else {
						if (mAuthorizeInterface != null) {
							mAuthorizeInterface.authorizeFailed("授权失败");
						}
					}
					return;
				}
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				Log.i("lijinzhe", "onReceivedError   url:" + failingUrl);
				if (mAuthorizeInterface != null) {
					mAuthorizeInterface.authorizeFailed(description);
				}
				super.onReceivedError(view, errorCode, description, failingUrl);
			}

			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed();
			}
		});
	}
}
